<%--
  Created by IntelliJ IDEA.
  User: Cc
  Date: 2020/12/30
  Time: 16:51
  To change this template use File | Settings | File Templates.
--%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" +
            request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>添加</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="shortcut icon" href="/static/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" href="/static/css/font.css">
    <link rel="stylesheet" href="/static/css/xadmin.css">
    <script type="text/javascript" src="/static/js/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="/static/lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="/static/js/xadmin.js"></script>
</head>

<body>
<div class="x-body">
    <form class="layui-form">
        <div class="layui-form-item">
            <label for="mType" class="layui-form-label">
                <span class="x-red">*</span>型号
            </label>
            <div class="layui-input-inline">
                <input type="text" id="mType" name="mType" onblur="findGoodsModelBymType();"
                       autocomplete="off" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span id="mMsg"  class="x-red"></span>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="mName" class="layui-form-label">
                名字
            </label>
            <div class="layui-input-inline">
                <input type="text" id="mName" name="mName"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label for="mRemarks" class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea id="mRemarks" name="mRemarks" placeholder="请输入内容" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <label for="L_repass" class="layui-form-label">
            </label>
            <button type="button"  class="layui-btn" id="L_repass" onclick="updateGoodsModel();">
                更新
            </button>
        </div>
    </form>
</div>
<script>
    $(function () {
        $("#mType").val(localStorage.getItem("mType"));
        $("#mName").val(localStorage.getItem("mName"));
        $("#mRemarks").val(localStorage.getItem("mRemarks"));
    })
    function updateGoodsModel() {
        if($("#mType").val().trim()==""){
            layer.alert("型号不许为空！",{icon:5})
        }else {
            $.ajax({
                url:"../gm/updateGoodsModelById",
                type:"POST",
                async:false,
                data:{
                    "mId":localStorage.getItem("mId"),
                    "mType":$("#mType").val(),
                    "mName":$("#mName").val(),
                    "mRemarks":$("#mRemarks").val(),
                },
                dataType:"json",
                success:function (data) {
                    if (data.code == 200) {
                        layer.alert("更新成功",{icon:6},function () {
                            // 获得frame索引
                            let index = parent.layer.getFrameIndex(window.name);
                            //刷新父页面
                            parent.location.reload();
                            //关闭当前frame
                            parent.layer.close(index);
                        })
                    }else {
                        layer.alert("更新失败",{icon:5})
                    }
                },error:function () {
                    layer.alert("服务器错误",{icon:7});
                }
            })
        }
    }
    function findGoodsModelBymType() {
        if($("#mType").val()!=localStorage.getItem("mType")){
            $.ajax({
                url:"../gm/findGoodsModelBymType",
                type:"GET",
                data:{
                    "mType":$("#mType").val(),
                },
                success:function (data) {
                    if (data.code == 200){
                        layer.alert("型号"+$("#mType").val()+"已存在",{icon:5});
                        $("#mMsg").html("该型号已存在");
                        $("#L_repass").addClass("layui-btn-disabled");
                        $("#L_repass").attr("disabled",true);
                    }else{
                        $("#mMsg").html("");
                        $("#L_repass").attr("disabled",false);
                        $("#L_repass").removeClass("layui-btn-disabled");
                    }
                }
            })
        }else{
            $("#mMsg").html("");
            $("#L_repass").attr("disabled",false);
            $("#L_repass").removeClass("layui-btn-disabled");
        }
    }
</script>
</body>
</html>